package com.qire.manhua.controller;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.activity.ComicSearchActivity;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel.SearchHotItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel.SearchGuessItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel.SearchResultItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel.SearchHistoryItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel.SearchRecommendItemViewModel;

import net.yslibrary.android.keyboardvisibilityevent.util.UIUtil;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class ComicSearchController extends EventController<ComicSearchActivity> implements AutoVariable {

    private ComicSearchViewModel viewModel;

    public ComicSearchController(@NonNull ComicSearchActivity activity) {
        super(activity);
    }

    @Override
    protected void bindView() {
        owner.viewDataBinder().setComicSearchController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.comicSearchController,this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicSearchViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 软键盘键盘搜索按钮响应事件
     */
    public boolean searchWordsOnEditorAction(TextView view, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            viewModel.searchComic();
            UIUtil.hideKeyboard(owner);
            return true;
        }
        return false;
    }

    /**
     * 清除搜索词按钮点击事件
     */
    public void clearSearchWordsOnClick(View view) {
        viewModel.setSearchWords("");
    }

    /**
     * 取消按钮点击事件
     */
    public void cancelBtnOnClick(View view) {
        owner.onBackPressed();
    }

    /**
     * 搜索按钮点击事件
     */
    public void searchBtnOnClick(View view) {
        viewModel.searchComic();
        UIUtil.hideKeyboard(owner);
    }

    /**
     * 历史清除按钮点击事件
     */
    public void clearHistoryOnClick(View view) {
        if(viewModel.isSearchHistoryEmpty()){
            ToastHelper.showToastWithoutDelay("记录为空,无需删除");
            return;
        }
        DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_l)
                .setTextView(R.id.dialog_title, "删除提示")
                .setTextView(R.id.dialog_content, "是否删除搜索历史？")
                .setTextView(R.id.dialog_button_light, "确定", confirmView -> {
                    viewModel.clearHistoryData();
                    DialogFastBuildUtil.dismissDialogBy(confirmView);
                })
                .setTextView(R.id.dialog_button_dark, "取消", cancelView -> DialogFastBuildUtil.dismissDialogBy(cancelView))
                .show();
    }

    /**
     * 热门搜索词点击事件
     */
    public void hotItemOnClick(View view) {
        SearchHotItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), SearchHotItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId())
                    .navigation();
        }
    }

    /**
     * 历史搜索词点击事件
     */
    public void historyItemOnClick(View view) {
        SearchHistoryItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), SearchHistoryItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            viewModel.setSearchWords(itemViewModel.searchWords);
            viewModel.searchComic();
        }
    }

    /**
     * 搜索推荐点击事件
     */
    public void recommendItemOnClick(View view) {
        SearchRecommendItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), SearchRecommendItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId)
                    .navigation();
        }
    }

    /**
     * 搜索结果点击事件
     */
    public void resultItemOnClick(View view) {
        SearchResultItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), SearchResultItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId)
                    .navigation();
        }
    }

    /**
     * 联想词点击事件
     */
    public void guessItemOnClick(View view) {
        SearchGuessItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), SearchGuessItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.saveSearchRecommend();
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId())
                    .navigation();
        }
    }

}
